package echat.future.cn.echat.my.weight.picselector;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.adapter.ImageSelecotrAdapter;
import echat.future.cn.echat.my.bean.ImageBean;

/**
 * 图片选择，删除UI
 */
public class PicSelectorView extends LinearLayout {

    private int lineCount = 3;
    private int maxCount = 3;

    ImageSelecotrAdapter imageSelecotrAdapter;

    /**
     * 选中的图片地址
     */
    public List<ImageBean> selectorUrls = new ArrayList<>();


    private RecyclerView recyclerView;

    public PicSelectorView(Context context) {
        this(context, null);
    }

    public PicSelectorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PicSelectorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PicSelectorView);
        maxCount = typedArray.getInt(R.styleable.PicSelectorView_maxNumbers, maxCount);
        lineCount = typedArray.getInt(R.styleable.PicSelectorView_lineCount, lineCount);
        typedArray.recycle();


        View rootView = LayoutInflater.from(context).inflate(R.layout.layout_pic_selector_view, null, false);
        rootView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        recyclerView = rootView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(context,lineCount));
        selectorUrls.add(new ImageBean());
        imageSelecotrAdapter = new ImageSelecotrAdapter(selectorUrls);
        recyclerView.setAdapter(imageSelecotrAdapter);
        imageSelecotrAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if(view.getId() == R.id.add){
                choosePic();
            }
            if(view.getId() == R.id.delete){
                selectorUrls.remove(position);
                imageSelecotrAdapter.notifyDataSetChanged();
                if(onPicCountChangeListenre !=null){
                    onPicCountChangeListenre.onPicCountChangeListener();
                }
            }
        });
        addView(rootView);
    }


    public List<ImageBean> getSelectorUrls() {
        return selectorUrls;
    }


    private void choosePic() {
        PictureSelector.create((Activity) getContext())
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(maxCount-selectorUrls.size()+1)
                .enableCrop(true)
                .withAspectRatio(1, 1)
                .isCamera(false)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                // 图片选择结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                for(int i=0;i<selectList.size();i++) {
                    LocalMedia media = selectList.get(i);
                    String headImgPath = "";
                    if (media.isCut()) {
                        headImgPath = media.getCutPath();
                    } else {
                        headImgPath = media.getPath();
                    }
                    selectorUrls.add(new ImageBean(headImgPath));
                    imageSelecotrAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    OnPicCountChangeListenre onPicCountChangeListenre;

    public void setOnPicCountChangeListenre(OnPicCountChangeListenre onPicCountChangeListenre) {
        this.onPicCountChangeListenre = onPicCountChangeListenre;
    }

    public interface OnPicCountChangeListenre{
        void onPicCountChangeListener();
    }
}
